package com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.resource.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.bean.LimsControlmaterialBean;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.resource.LimsControlmaterialResource;
import com.sunwayworld.basemodule.business.qualitycontrol.controlmaterial.service.LimsControlmaterialService;
import com.sunwayworld.framework.log.annotation.Log;
import com.sunwayworld.framework.log.annotation.LogModule;
import com.sunwayworld.framework.log.annotation.LogType;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@LogModule("质控品管理记录表")
@RestController
@GikamBean
public class LimsControlmaterialResourceImpl implements LimsControlmaterialResource {

    @Autowired
    private LimsControlmaterialService limsControlmaterialService;

    @Override
    public LimsControlmaterialService getService() {
        return limsControlmaterialService;
    }

    @Log(value = "新增质控品管理记录表", type = LogType.INSERT)
    @Override
    @RequestMapping(method = RequestMethod.POST)
    public Long insert(RestJsonWrapperBean wrapper) {
        return getService().insert(wrapper);
    }

    @Override
    public String activeOn(RestJsonWrapperBean bean) {
        List<Long> ids = bean.getBodyList().get(0).stream().map(JSON::parseObject).collect(Collectors.toList())
                .stream().map(item -> item.getLong("id")).collect(Collectors.toList());
        LimsControlmaterialBean update = new LimsControlmaterialBean();
        update.setActiveStatus("on");
        if (!ids.isEmpty()) {
            getService().getDao().updateByIds(update, ids, "activeStatus");
        }
        return null;
    }

    @Override
    public String activeOff(RestJsonWrapperBean bean) {
        List<Long> ids = bean.getBodyList().get(0).stream().map(JSON::parseObject).collect(Collectors.toList())
                .stream().map(item -> item.getLong("id")).collect(Collectors.toList());
        LimsControlmaterialBean update = new LimsControlmaterialBean();
        update.setActiveStatus("off");
        if (!ids.isEmpty()) {
            getService().getDao().updateByIds(update, ids, "activeStatus");
        }
        return null;
    }

    @Override
    public JSONObject checkIfStillInUse(RestJsonWrapperBean bean) {
        return getService().checkIfStillInUse(bean);
    }
}
